package cz.fabian.android.placeLocator;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class MainActivity extends ListActivity {
	private DbAdapter helper;
	private Cursor cursor;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // register this view to handle long press actions
        registerForContextMenu(getListView());
        
        helper = new DbAdapter(this);
        helper.open();
        cursor = helper.fetchAll();
        startManagingCursor(cursor);

        if (cursor.getCount() == 0) {
        	Toast toast = Toast.makeText(
    			getBaseContext(), getResources().getString(R.string.new_place_hint), Toast.LENGTH_LONG
        	);
        	toast.show();
        }
        
        //TODO: acquire current location and sort items in list
        //Location location = locManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
        
        String[] from = new String[]{DbAdapter.KEY_TITLE};
        int[] to = new int[]{android.R.id.text1};
        setListAdapter(new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, cursor, from, to));
    }
    
    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	Intent i = new Intent(this, PlaceActivity.class);
    	i.putExtra("rowId", id);
    	startActivity(i);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
    	super.onCreateOptionsMenu(menu);
        menu.add(0, 1, 0, R.string.new_place);
        
        return true;
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case 1:
        	Intent i = new Intent(getBaseContext(), AddActivity.class);
    		startActivity(i);
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }
    
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    	super.onCreateContextMenu(menu, v, menuInfo);
    	
    	// get id of (long press) selected item
    	AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
    	long id = getListAdapter().getItemId(info.position);
    	
    	Cursor place = helper.fetch(id);
    	
    	menu.setHeaderTitle(place.getString(place.getColumnIndexOrThrow(DbAdapter.KEY_TITLE)));
//    	menu.add(0, 1, 0, R.string.view);
    	menu.add(0, 2, 0, R.string.delete);
    }
    
    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
    	long id = getListAdapter().getItemId(info.position);

    	switch (item.getItemId()) {
    	//FIXME: get context for intent
//    	case 1:
//    		Intent i = new Intent(this.get, PlaceActivity.class);
//        	i.putExtra("rowId", id);
//        	startActivity(i);
    	case 2:
    		//TODO: dialog window for confirm
    		helper.delete(id);
    		return true;
		default:
			return super.onContextItemSelected(item);
    	}
    }
    
    @Override
    public void onContextMenuClosed(Menu menu) {
    	super.onContextMenuClosed(menu);
    	
    	// reload data
    	cursor.requery();
    }
}